package com.guage.controller;

import java.util.HashMap;

import javax.validation.Valid;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guage.commons.base.BaseController;
import com.guage.commons.result.PageInfo;
import com.guage.model.ShopGoodsCategory;
import com.guage.service.IShopGoodsCategoryService;

/**
 * <p>
 * 商品分类表 前端控制器
 * </p>
 *
 * @author dechao.yang
 * @since 2017-12-14
 */
@Controller
@RequestMapping("/shopGoodsCategory")
public class ShopGoodsCategoryController extends BaseController {

    @Autowired 
    private IShopGoodsCategoryService shopGoodsCategoryService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/shop/shopGoodsCategory/shopGoodsCategoryList";
    }
    
    
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(Integer page, Integer rows, String sort, String order) {
    	 PageInfo pageInfo = new PageInfo(page, rows, sort, order);
    	 HashMap<String, Object> condition = new HashMap<String, Object>();
         condition.put("categoryParentId",NumberUtils.BYTE_ZERO);
         pageInfo.setCondition(condition);
         shopGoodsCategoryService.selectPageDataByParams(pageInfo);
         return pageInfo;
    }
    
    @GetMapping("/secondCategoryPage")
    public String secondCategoryPage(Model model, Long id) {
    	ShopGoodsCategory shopGoodsCategory = shopGoodsCategoryService.selectById(id);
        model.addAttribute("shopGoodsCategory", shopGoodsCategory);
        return "admin/shop/shopGoodsCategory/shopGoodsSecondCategoryList";
    }
    @GetMapping("/threeCategoryPage")
    public String threeCategoryPage(Model model, Long id) {
    	ShopGoodsCategory shopGoodsCategory = shopGoodsCategoryService.selectById(id);
        model.addAttribute("shopGoodsCategory", shopGoodsCategory);
        return "admin/shop/shopGoodsCategory/shopGoodsThreeCategoryList";
    }
    
    @PostMapping("/dataSecondGrid")
    @ResponseBody
    public Object dataSecondGrid(@Valid ShopGoodsCategory shopGoodsCategory,Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        if(shopGoodsCategory.getCategoryId()!=null){
        	 HashMap<String, Object> condition = new HashMap<String, Object>();
             condition.put("categoryParentId", shopGoodsCategory.getCategoryId());
             pageInfo.setCondition(condition);
        }
        shopGoodsCategoryService.selectPageDataByParams(pageInfo);
        return pageInfo;
    }
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/shop/shopGoodsCategory/shopGoodsCategoryAdd";
    }
    /**
     * 二级分类添加框
     * @return
     */
    @GetMapping("/addSecondCategoryPage")
    public String addSecondCategoryPage(Model model, Long id) {
    	 model.addAttribute("parentCategoryId", id);
        return "admin/shop/shopGoodsCategory/shopGoodsSecondCategoryAdd";
    }
    /**
     * 三级分类添加框
     * @return
     */
    @GetMapping("/addThreeCategoryPage")
    public String addThreeCategoryPage(Model model, Long id) {
    	/**
    	 * 三级分类父级id
    	 */
    	//model.addAttribute("threeCategoryParentId", id);
    	ShopGoodsCategory shopGoodsCategory = shopGoodsCategoryService.selectById(id);
    	model.addAttribute("shopGoodsCategory", shopGoodsCategory);
        return "admin/shop/shopGoodsCategory/shopGoodsThreeCategoryAdd";
    }
     /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(@Valid ShopGoodsCategory shopGoodsCategory) {
    	 shopGoodsCategoryService.insert(shopGoodsCategory);
         return renderSuccess("添加成功！");
    }
    
    
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
    	  shopGoodsCategoryService.deleteById(id);
          return renderSuccess("删除成功！");
    }
    
    
     /**
     * 编辑页面
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {    	  	
        ShopGoodsCategory shopGoodsCategory = shopGoodsCategoryService.selectById(id);
        model.addAttribute("shopGoodsCategory", shopGoodsCategory);
        return "admin/shop/shopGoodsCategory/shopGoodsCategoryEdit";
    }
    
    /**
     * 三级菜单编辑页面
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editThreeCategoryPage")
    public String editThreeCategoryPage(Model model, Long id) {    	  	
        ShopGoodsCategory shopGoodsCategory = shopGoodsCategoryService.selectById(id);
        model.addAttribute("shopGoodsCategory", shopGoodsCategory);
        return "admin/shop/shopGoodsCategory/shopGoodsThreeCategoryEdit";
    }
    /**
     * 三级菜单编辑页面
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editSecondCategoryPage")
    public String editSecondCategoryPage(Model model, Long id) {    	  	
        ShopGoodsCategory shopGoodsCategory = shopGoodsCategoryService.selectById(id);
        model.addAttribute("shopGoodsCategory", shopGoodsCategory);
        return "admin/shop/shopGoodsCategory/shopGoodsSecondCategoryEdit";
    }
    
    
    /**
     * 更新
     * @param 
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Object edit(@Valid ShopGoodsCategory shopGoodsCategory) {
    	shopGoodsCategoryService.updateById(shopGoodsCategory);
        return renderSuccess("编辑成功！");
    }
    
    
}
